package com.yst.gateway.monitor.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.groups.Default;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yst.gateway.monitor.common.beans.Response;
import com.yst.gateway.monitor.common.validator.groups.Groups;
import com.yst.gateway.monitor.models.SysRoles;
import com.yst.gateway.monitor.services.SysRoleService;


@RestController
@RequestMapping(value = "/sys/roles")
public class SysRoleController {
	
	
	@Autowired
	private SysRoleService sysRoleService;
	
	
	@RequestMapping(value = "/add" , method = RequestMethod.POST)
	public Response add(@RequestBody @Validated({Default.class,Groups.Add.class}) SysRoles sysRoles) {
		Response response = new Response();
		if (sysRoleService.add(sysRoles)) {
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	
	
	@RequestMapping(value = "/update",method = RequestMethod.PUT)
	public Response update(@RequestBody @Validated({Default.class,Groups.Modify.class}) SysRoles sysRoles){
		Response response = new Response();
		if (sysRoleService.update(sysRoles)) {
			response.success();
		} else {
			response.failure();
		}
		
		return response;
	}
	
	
	@RequestMapping(value = "/upStatus/{id}" ,method = RequestMethod.PUT)
	public Response upStatus(@PathVariable("id") String id) {
		Response response = new Response();
		if (sysRoleService.updateStatusById(id)){ 
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	
	
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	public Response getAll(@RequestParam Map<String, Object> paramMap) {
		Response response=new Response();
		response.success(sysRoleService.getByMap(paramMap));
		return response;
	}
	
	
	@RequestMapping(value = "/get/{id}" ,method = RequestMethod.GET)
	public Response getById(@PathVariable("id") String id) {
		Response response = new Response();
		SysRoles sysRoles = sysRoleService.getById(id);
		if (sysRoles != null){ 
			response.success(sysRoles);
		} else {
			response.failure();
		}
		return response;
	}
	
	
	@RequestMapping(value = "/checkName", method = RequestMethod.GET)
	public boolean checkName(@RequestParam String name) {
		List<SysRoles> list = sysRoleService.selectByName(name);
		if (list == null || list.size() == 0) {
			return true;
		} 
		return false;
	}
	
	
	
	
	
	@RequestMapping(value = "/checkEditName", method = RequestMethod.GET)
	public boolean checkEditName(HttpServletRequest request) {
		SysRoles sysRoles = sysRoleService.getById(request.getParameter("id"));
		String [] names = request.getParameterValues("name");
		String edtiName = names[1];
		if (sysRoles != null && edtiName.equals(sysRoles.getName())) {
			return true;
		} else {
			List<SysRoles> list = sysRoleService.selectByName(edtiName);
			if (list == null || list.size() == 0) {
				return true;
			} 
			return false;
		}
	}
	
	
	@RequestMapping(value = "/getByPage", method = RequestMethod.GET)
	public Response getByPage (@RequestParam Map<String ,Object> paramMap) {
		Response response = new Response();
		int pageNum = 1;
		int pageSize = 10;
		try{
			if(paramMap.containsKey("pageNum")){
				pageNum=Integer.parseInt((String)paramMap.get("pageNum"));
				paramMap.remove("pageNum");
			}
			if(paramMap.containsKey("pageSize")){
				pageSize=Integer.parseInt((String)paramMap.get("pageSize"));
				paramMap.remove("pageSize");
			}
		} catch (Exception e) {
				
			}
		response.success(sysRoleService.getByPage(pageNum, pageSize, paramMap));
		return response;
	}
	
	
	@RequestMapping(value = "/del/{id}",method = RequestMethod.PUT)
	public Response del (@PathVariable("id") String id) {
		Response response = new Response();
		if (sysRoleService.delById(id)) {
			response.success();
		} else {
			response.failure();
		}
		return response;
	}
	


}
